package com.shanghaionstar.servicepackage.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.payme.response.GetPackageListResponse;
import com.shanghaionstar.servicepackage.model.OnstarPkgModel;
import com.shanghaionstar.servicepackage.view.OnstarPkgView;

import rx.functions.Action1;

/**
 * 获取安吉星套餐列表信息
 * Created by Yonglang Hong on 2016/8/8.
 */
public class OnstarPkgPresenter extends BasePresenter {

    private OnstarPkgView packageView;

    private OnstarPkgModel packageModel;

    public OnstarPkgPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        packageModel = new OnstarPkgModel(context);
    }

    public void setPackageView(OnstarPkgView packageView) {
        this.packageView = packageView;
    }

    public void getOnstarPackages() {
        subscription = packageModel.loadPackageList()
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<GetPackageListResponse>() {
                    @Override
                    public void call(GetPackageListResponse response) {
//                        UserCenterCacheHelper.getInstance().setPackageEntity_onstar(response);
                        if (packageView != null) {
                            packageView.showPackageList(response);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (packageView != null && throwable != null) {
                            throwable.printStackTrace();
                            packageView.showError(throwable.getMessage());
                        }
                    }
                });
    }
}
